package com.google.hackathon.game.shriko.core;

import static playn.core.PlayN.*;

import org.jbox2d.collision.shapes.CircleShape;
import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.Body;
import org.jbox2d.dynamics.BodyDef;
import org.jbox2d.dynamics.BodyType;
import org.jbox2d.dynamics.FixtureDef;
import org.jbox2d.dynamics.World;

import playn.core.Image;
import playn.core.ImageLayer;
import playn.core.Layer;

class Syuriken {
  private ImageLayer layer;
  private float angle = 0.0f;
  private float x = 0.0f;
  private float y = 0.0f;
  private Body body;

  public Syuriken(World world, Image image, float x, float y) {
    layer = graphics().createImageLayer(image);
    layer.setOrigin(image.width() / 2f, image.height() / 2f);
    layer.setTranslation(x, y);
    this.x = x;
    this.y = y;

    final BodyDef bodyDef = new BodyDef();
    bodyDef.type = BodyType.DYNAMIC;
    bodyDef.position = new Vec2(x, y);
    body = world.createBody(bodyDef);
    FixtureDef fixtureDef = new FixtureDef();
    // TODO(bonbori): fix
    fixtureDef.shape = new CircleShape();
    fixtureDef.shape.m_radius = image.width() / 2f;
    fixtureDef.density = 1.0f;
    fixtureDef.friction = 0.3f;
    body.createFixture(fixtureDef);
  }

  public void update(float delta) {
    layer.setRotation(angle);
    y -= delta;
    layer.setTranslation(x, y);
    angle += delta;
  }

  public Layer getLayer() {
    return layer;
  }
}
